package com.kyub.spring.gears.services.impl;

import com.kyub.spring.gears.services.FileStorageService;
import com.kyub.spring.gears.model.FileLayout;

import java.io.IOException;
import java.io.Serializable;
import java.io.File;
import java.util.Map;
import java.util.HashMap;


public class InMemoryStorageService implements FileStorageService{

    private static final Map<String, byte[]> files = new HashMap<String,byte[]>();


    @Override
    public byte[] load(String url) throws IOException {
        return files.get(url);
    }

    @Override
    public void save(String pojoName, Serializable id, String propertyName, FileLayout fileLayout, String originalFilename, byte[] payload) throws IOException {
        String ids = "";
        if (id != null)
            ids = String.valueOf(id);
        final String path = fileLayout.getPersistPath() + "/" + pojoName + "_" + ids + "_" + propertyName + "_" + System.currentTimeMillis() + "." + fileLayout.getExtension();
       files.put(path,payload);
    }
}
